const app = getApp();
import Toast from 'tdesign-miniprogram/toast/index';
Page({
     // 轻提示
     toast(option) {
        Toast({
            context: this,
            selector: '#t-toast',
            ...option,
        });
    },
	data: {
        swiperList: [],
        dataList: [],
        pageInfo: {
            pageNum: 1,
            pageSize: 5,
        },
        openid: ``,
        hasMoreData: true,
	},
	onLoad(options) {
        if (options.openid) {
            this.setData({openid: options.openid});
        }
        this.findPage();
    },
    
    findPage() {
        // 开启加载效果
        this.toast({
            message: '加载中...',
            theme: 'loading',
            direction: 'column',
            duration: 1000
        });
        app.httpPost(`wish/page`, { page: this.data.pageInfo, wish: { openid: this.data.openid } }).then((res) => {
            // 定义原有数据
            let temp = this.data.dataList;
            // 接收新的结果
            let result = res.rows;
            if (result && result.length > 0) {
                // 用户下拉
                if (this.data.pageInfo.pageNum == 1) {
                    temp = [];
                }
                const rows = result.filter(item => {
                    if (item.images) {
                        item.images = item.images.split(',').map(i => (app.getImageUrl(i)))
                    }
                    if (item.avatar) {
                        item.avatar = app.getImageUrl(item.avatar);
                    }
                    if (item.createdAt) {
                        item.createdAt = app.dateTimeFormat(item.createdAt);
                    }
                    return true;
                });
                // 如果结果长度大于分页数，则说明还可以上拉
                if (result.length < this.data.pageInfo.pageSize) {
                    this.setData({ hasMoreData: false, dataList: temp.concat(rows) });
                } else {
                    this.setData({ hasMoreData: true, dataList: temp.concat(rows), pageInfo: { pageNum: this.data.pageInfo.pageNum + 1, pageSize
                    : 5 } });
                }
            } else {
                this.setData({ hasMoreData: false });
            }
        });
    },

	/**
	 * 初始化轮播
	 */
	initSwiper() {
		app.httpGet(`carousel/index`).then(({ data }) => {
			this.setData({ swiperList: data.map((i) => (app.getImageUrl(i.url))) });
		});
	},

	onShow: function () {
        this.initSwiper();
		this.getTabBar().init();
    },
    
    /**
     * 用户下拉操作
     */
    onPullDownRefresh() {
        this.setData({ hasMoreData: true });
        this.setData({ pageInfo: {
            pageNum: 1,
            pageSize: 5,
        }});
        this.findPage();
    },

    /**
     * 用户上拉
     */
    onReachBottom: function() {
        if (this.data.hasMoreData) {
            this.findPage();
        } else {
            this.toast({
                message: '没有跟多数据了！',
                theme: 'success',
                duration: 1000
            });
        }
    }
})